package dlally.model;

import java.util.ArrayList;
/**
 * A line is a structure used to keep track of one choice, the position of that choice, and all the edges attached to this line
 * @author Douglas Lally (dlally@wpi.edu)
 *
 */
public class Line {
	
	/**
	 * Every line will have to have a choice, a position, and a list of edges of unknown length
	 */
	String choice;
	int position;
	ArrayList<Edge> edges;
	
	/**
	 * Simple constructor
	 * @param choice the name of the choice for this line
	 * @param position the index of this line
	 */
	public Line(int position) {
		super();
		this.choice = "";
		this.position = position;
		this.edges = new ArrayList<Edge>();
	}
	
	/**
	 * Gets the edge that comes after the specified height
	 * @param height the height after which you want to find the next edge
	 * @return the next edge, or null if no edges exist on this line
	 */
	Edge nextEdge(int height){
		// If there are no edges, just return null
		if(edges.size() == 0)
			return null;
		Edge tempEdge = null;
		// Look at every edge. Figure out the top and bottom.
			for(int i = 0; i < edges.size() && tempEdge == null; i++){
				if(height < edges.get(i).height)
					tempEdge = edges.get(i);
			}
			return tempEdge;
		}
	
	/**
	 * Adds the edge to the list of edges so that the list of edges is still in order
	 * @param e the edge to add to this line
	 */
	public void addEdgeInOrder(Edge e){
		int currentSize = edges.size();
		// If there are no edges, just add it
		if(currentSize == 0)
			edges.add(e);
		// If it should go at the end of the list, add it there
		else if(edges.get(edges.size() - 1).getHeight() < e.getHeight())
			edges.add(e);
		else{
			for(int i = 0; i < edges.size() && currentSize == edges.size(); i++){
				if(edges.get(i).getHeight() > e.getHeight()){
					edges.add(i,e);
				}
			
			}
		}
	}
	
	/**
	 * 
	 * @return the number of edges attached to this line
	 */
	public int numEdges(){
		return edges.size();
	}
		
	/**
	 * @return the choice
	 */
	public String getChoice() {
		return choice;
	}
	
	/**
	 * @return the position of the line
	 */
	public int getPosition() {
		return position;
	}

	/**
	 * @return the list of edges
	 */
	public ArrayList<Edge> getEdges() {
		return edges;
	}
	
	/**
	 * @param name the name of the choice to be set
	 */
	public void setChoice(String name){
		this.choice = name;
	}
	
	
	
	

}
